/**
 * */
Parse.Cloud.define('editAdminRole', async (req, res) => {
  const user = req.user
  const permission = {sessionToken: user.getSessionToken()}
  const {id, role, reduceRole} = req.params
  const query = new Parse.Query(Parse.User)
  query.equalTo('objectId', id)
  try {
    const resultUser = await query.first(permission)
    const roles = role.map(async item => {
      const query = new Parse.Query(Parse.Role)
      query.equalTo('objectId', item)
      const resultRole = await query.first(permission)
      resultRole.getUsers().add(resultUser)
      return resultRole.save(null, permission)
    })
    const reduceRoles = reduceRole.forEach(async item => {
      const query = new Parse.Query(Parse.Role)
      query.equalTo('objectId', item)
      const resultRole = await query.first(permission)
      resultRole.getUsers().remove(resultUser)
      return resultRole.save(null, permission)
    })
    const allRoles = roles.concat(reduceRoles)
    for (let i = 0, len = allRoles.length; i < len; i++) {
      await allRoles[i]
    }
    res.success('OK')
  } catch (err) {
    console.log(err)
    res.error(err.message)
  }
})
